/**
 * 
 */
package com.sx.form.ts.biz.impl;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONArray;
import com.sx.article.entity.AttachmentEntity;
import com.sx.feesunit.exception.SXException;
import com.sx.form.ts.biz.IHouseInfoBiz;
import com.sx.form.ts.dao.IHouseInfoDao;
import com.sx.form.ts.entity.HouseInfoAndAttachment;
import com.sx.form.ts.entity.HouseInfoEntity;

/**
 * @author Administrator
 *
 */
public class HouseInfoBiz implements IHouseInfoBiz {
	private IHouseInfoDao houseinfodao;
	private Log log = LogFactory.getLog(this.getClass());
	
	/* (non-Javadoc)
	 * @see com.sx.form.ts.biz.IHouseInfoBiz#findHouseInfoForInfosPage(com.sx.form.ts.entity.HouseInfoEntity)
	 */
	public List<HouseInfoEntity> findHouseInfoForInfosPage(
			HouseInfoEntity houseinfo) throws SXException {
		return this.findHouseInfo(houseinfo, "HouseInfo.findHouseInfoForInfosPage");
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.biz.IHouseInfoBiz#editHouseInfoOnly(com.sx.form.ts.entity.HouseInfoEntity)
	 */
	public boolean editHouseInfoOnly(HouseInfoEntity houseinfo)
			throws SXException {
		boolean flag = false;
		try {
			flag = houseinfodao.editHouseInfo(houseinfo);
		} catch (Exception e) {
			log.error("按编号编辑房源信息",e);
			throw new SXException();
		}
		return flag;
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.biz.IHouseInfoBiz#deleteHouseInfoById(com.sx.form.ts.entity.HouseInfoEntity)
	 */
	public boolean deleteHouseInfoById(HouseInfoEntity houseinfo)
			throws SXException {
		boolean flag = false;
		try {
			flag = houseinfodao.deleteHouseInfoById(houseinfo);
		} catch (Exception e) {
			log.error("按编号删除房源信息",e);
			throw new SXException();
		}
		return flag;
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.biz.IHouseInfoBiz#deleteHouseInfoAndKeyImageInfo(java.lang.Integer)
	 */
	public boolean deleteHouseInfoAndKeyImageInfo(Integer fid)
			throws SXException {
		boolean flag = false;
		try {
			flag = houseinfodao.deleteHouseInfoAndKeyImageInfo(fid);
		} catch (Exception e) {
			log.error("按附件编号删除房源与KeyImage关联信息",e);
			throw new SXException();
		}
		return flag;
	}
	/**
	 *按附件编号删除房源与附件关联信息 
	 **/
	public boolean deleteHouseInfoAndAttachmentInfo(Integer fid) throws SXException{
		boolean flag = false;
		try {
			flag = houseinfodao.deleteHouseInfoAndAttachmentInfo(fid);
		} catch (Exception e) {
			log.error("按附件编号删除房源与附件关联信息",e);
			throw new SXException();
		}
		return flag;
	}
	/**
	 * 按编号查询房源信息
	 * 
	 **/
	public HouseInfoEntity getHouseInfoById(HouseInfoEntity houseinfo) throws SXException{
		HouseInfoEntity house = null;
		try {
			house = houseinfodao.getHouseInfoById(houseinfo);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("按编号查询房源信息",e);
			throw new SXException();
		}
		return house;
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.biz.IHouseInfoBiz#queryHouseInfoById(com.sx.form.ts.entity.HouseInfoEntity)
	 */
	public String queryHouseInfoById(HouseInfoEntity houseinfo)
			throws SXException {
		// TODO Auto-generated method stub
		String result = "";
		try {
			Integer count = houseinfodao.findHouseInfoCount(houseinfo);
			List<HouseInfoEntity> list = houseinfodao.findHouseInfo(houseinfo);
			HashMap<String,Object> map = new HashMap<String,Object>();
			Integer pagesize = count/houseinfo.getLimit();
			if(count%houseinfo.getLimit()!=0){
				pagesize = pagesize+1;
			}
			map.put("pageCount", pagesize);
			map.put("data", list);
			result=JSONArray.toJSONString(map);
		} catch (Exception e) {
			result = "{\"totalCount\":0,\"data\":[]}";
			log.error("带分页的数据信息",e);
			throw new SXException();
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see com.sx.form.ts.biz.IHouseInfoBiz#queryHouseInfoWithOutTotal(com.sx.form.ts.entity.HouseInfoEntity)
	 */
	public String queryHouseInfoWithOutTotal(HouseInfoEntity houseinfo)
			throws SXException {
		// TODO Auto-generated method stub
		String result = "";
		try {
			List<HouseInfoEntity> list = houseinfodao.findHouseInfo(houseinfo);
			result = JSONArray.toJSONString(list);
		} catch (Exception e) {
			result = "[]";
			// TODO: handle exception
			log.error("获取房源数据信息用滚动条进行分页",e);
			throw new SXException();
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see com.sx.form.ts.biz.IHouseInfoBiz#findHouseInfo(com.sx.form.ts.entity.HouseInfoEntity, java.lang.String)
	 */
	public List<HouseInfoEntity> findHouseInfo(HouseInfoEntity houseinfo,
			String queryinfo) throws SXException {
		List<HouseInfoEntity> list = null;
		try {
			list = this.houseinfodao.findHouseInfo(houseinfo,queryinfo);
		} catch (Exception e) {
			log.error("获取房源信息",e);
			throw new SXException();
		}
		return list;
	}
	/* (non-Javadoc)
	 * @see com.sx.form.ts.biz.IHouseInfoBiz#findHouseInfo(com.sx.form.ts.entity.HouseInfoEntity)
	 */
	public List<HouseInfoEntity> findHouseInfo(HouseInfoEntity houseinfo)
			throws SXException {
		List<HouseInfoEntity> list = null;
		try {
			list = this.houseinfodao.findHouseInfo(houseinfo);
		} catch (Exception e) {
			log.error("获取房源信息",e);
			throw new SXException();
		}
		return list;
	}

	/* (non-Javadoc)
	 * @see com.sx.form.ts.biz.IHouseInfoBiz#findHouseInfoCount(com.sx.form.ts.entity.HouseInfoEntity)
	 */
	public Integer findHouseInfoCount(HouseInfoEntity houseinfo)
			throws SXException {
		Integer id = null;
		try {
			id = this.houseinfodao.findHouseInfoCount(houseinfo);
		} catch (Exception e) {
			log.error("获取房源信息",e);
			throw new SXException();
		}
		return id;
	}

	/* (non-Javadoc)
	 * @see com.sx.form.ts.biz.IHouseInfoBiz#addHouseInfoCascadeRegion(com.sx.form.ts.entity.HouseInfoAndRegion)
	 */
	public Integer addHouseInfoCascadeRegion(HouseInfoEntity houseinfo) throws SXException {
		Integer id = null;
		try {
			id = houseinfodao.addHouseInfoCascadeRegion(houseinfo);
		} catch (Exception e) {
			log.error("为房源添加附件关联信息",e);
			throw new SXException();
		}
		return id;
	}

	/* (non-Javadoc)
	 * @see com.sx.form.ts.biz.IHouseInfoBiz#addHouseInfoCascadeFileIdInfo(com.sx.form.ts.entity.HouseInfoAndAttachment)
	 */
	public Integer addHouseInfoCascadeFileIdInfo(Integer houseid,Integer attachmentid)
			throws SXException {
		Integer id = null;
		try {
			HouseInfoAndAttachment hiaa = new HouseInfoAndAttachment(houseid,attachmentid);
			id = houseinfodao.addHouseInfoCascadeFileIdInfo(hiaa);
		} catch (Exception e) {
			log.error("为房源添加附件关联信息",e);
			throw new SXException();
		}
		return id;
	}
	
	/* (non-Javadoc)
	 * @see com.sx.form.ts.biz.IHouseInfoBiz#editHouseInfo(com.sx.form.ts.entity.HouseInfoEntity)
	 */
	public boolean editHouseInfo(HouseInfoEntity houseinfo) throws SXException {
		boolean flag = false;
		try {
			//0.向区域与房屋中间表中插入记录
			houseinfodao.addHouseInfoCascadeRegion(houseinfo);
			//1.向文件与房屋中间表里面插入记录
			if(houseinfo.getAttachmentid()!=null&&houseinfo.getAttachmentid().length>0){
				houseinfodao.addHouseInfoCascadeFileIdInfo(houseinfo, houseinfo.getList());
			}
			//2.向关键文件与房屋中间表里面插入记录
			AttachmentEntity keyimage = houseinfo.getKeyimage();
			if(houseinfo.getKeyimageid()!=null){
				houseinfodao.addHouseInfoCascadeKeyImageInfo(houseinfo, keyimage);
			}
			//3.向房屋特色中间信息表中插入记录
			houseinfodao.addHouseInfoCascadeSpecial(houseinfo, houseinfo.getSpecialid());
			//4.更新房屋中间表信息记录
			houseinfodao.editHouseInfo(houseinfo);
			flag = true;
		} catch (Exception e) {
			flag = false;
			log.error("修改房源信息出错",e);
			throw new SXException();
		}
		return flag;
	}

	/* (non-Javadoc)
	 * @see com.sx.form.ts.biz.IHouseInfoBiz#addHouseInfoDraft(com.sx.form.ts.entity.HouseInfoEntity)
	 */
	public Integer addHouseInfoDraft(HouseInfoEntity houseinfo)
			throws SXException {
		Integer id = null;
		try {
			id = this.houseinfodao.addHouseInfoDraft(houseinfo);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("暂存房源信息",e);
			throw new SXException();
		}
		return id;
	}
	
	/**
	 * @return the houseinfodao
	 */
	public IHouseInfoDao getHouseinfodao() {
		return houseinfodao;
	}
	/**
	 * @param houseinfodao the houseinfodao to set
	 */
	public void setHouseinfodao(IHouseInfoDao houseinfodao) {
		this.houseinfodao = houseinfodao;
	}
	
}
